package hz.mobileproject.pda.ui.activity.work;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.app.MainApp;
import hz.mobileproject.pda.entity.request.NoneTalkPush;
import hz.mobileproject.pda.entity.response.NoneTalkListBean;
import hz.mobileproject.pda.listener.CustomNetCallBack;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.ui.adapter.NoneTalkListAdapter;
import hz.mobileproject.pda.ui.base.BaseActivity;
import hz.mobileproject.pda.utils.HttpRqUtils;


@ContentView(R.layout.activity_edu_none_talk)
public class EduNoneTalkActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, OnLoadmoreListener {
    @ViewInject(R.id.rv_nontalk)
    private RecyclerView mRecyclerView;
    @ViewInject(R.id.srl_eduNoneTalk)
    private SmartRefreshLayout mSmartRefreshLayout;
    @ViewInject(R.id.checkBox)
    private CheckBox _checkBox;
    @ViewInject(R.id.bt_select_time)
    private ImageView iv_selectDate;

    private Gson mGson;
    private NoneTalkListAdapter mAdapter;
    //当前选择时间
    private String mSelectedDate;
    //页码
    private int mPage = 1;

    @Override
    protected void initData() {
        mGson = new Gson();
        _checkBox.setOnCheckedChangeListener(this);
        mSmartRefreshLayout.setOnLoadmoreListener(this);
        mAdapter = new NoneTalkListAdapter(this, new ArrayList<NoneTalkListBean>());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        //默认本月
        mSelectedDate = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM"));
        getNoneTalkListFromSer();
    }


    @Event({R.id.bt_select_time})
    private void viewOnClick(View v) {
        switch (v.getId()) {
            case R.id.bt_select_time:
                try {
                    DatePickDialog dialog = new DatePickDialog(EduNoneTalkActivity.this);
                    //回显当前选择时间
                    dialog.setStartDate(new SimpleDateFormat("yyyy-MM").parse(mSelectedDate));
                    //设置上下年分限制
                    dialog.setYearLimt(5);
                    //设置标题
                    dialog.setTitle("选择时间");
                    //设置类型
                    dialog.setType(DateType.TYPE_YM);
                    //设置消息体的显示格式，日期格式
                    dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                    //设置选择回调
                    dialog.setOnChangeLisener(null);
                    //设置点击确定按钮回调
                    dialog.setOnSureLisener(new OnSureLisener() {
                        @Override
                        public void onSure(Date date) {
                            mPage = 1;
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                            mSelectedDate = sdf.format(date);
                            //点击选择时间后 重新请求网络
                            getNoneTalkListFromSer();
                        }
                    });
                    dialog.show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }


    /**
     * 选择是否跟踪处置筛选
     *
     * @param buttonView
     * @param isChecked
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mPage = 1;
        getNoneTalkListFromSer();
    }


    /**
     * 下拉加载更多
     *
     * @param refreshlayout
     */
    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        getNoneTalkListFromSer();
    }

    /**
     * 调用接口获取列表
     */
    private void getNoneTalkListFromSer() {
        //不是下拉加载清空列表
        if (!mSmartRefreshLayout.isLoading()) {
            mAdapter.clearData();
        }
        NoneTalkPush noneTalkPush = new NoneTalkPush();
        noneTalkPush.setCusNumber(new String(MainApp.getInstance().getLoginUser().getCus_number() + ""));
        noneTalkPush.setPoliceId(new String(MainApp.getInstance().getLoginUser().getPolice_id()));
        noneTalkPush.setPoliceCode(new String(MainApp.getInstance().getLoginUser().getPolice_code()));
        noneTalkPush.setQueryDept(new String(MainApp.getInstance().getLoginUser().getDepartment_id() + ""));
        noneTalkPush.setIsStep(_checkBox.isChecked());
        noneTalkPush.setQueryTime(mSelectedDate);
        noneTalkPush.setQueryPage(mPage + "");
        HttpRqUtils.httpPost(Constance.BASE_URL + Constance.URL_EDUTALK_QUERY_NOTALK, noneTalkPush, noneTalkCallBack);
    }


    /**
     * 获取列表回调
     */
    private CustomNetCallBack noneTalkCallBack = new CustomNetCallBack() {
        @Override
        public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {
            LogUtils.e(data);
            List<NoneTalkListBean> listData = mGson.fromJson(data, new TypeToken<List<NoneTalkListBean>>() {
            }.getType());
            if (mSmartRefreshLayout.isLoading()) {
                mAdapter.addData(listData);
                mSmartRefreshLayout.finishLoadmore();
            } else {
                mAdapter.clearAddAll(listData);
            }
            if (listData == null || listData.size() <= 0) {
                ToastUtils.showLong("没有更多数据了");
                return;
            }
            mPage++;
        }

        @Override
        public void onErrorFromSer(int rtnCode, String rtnMsg) {
            if (mSmartRefreshLayout.isLoading()) {
                mSmartRefreshLayout.finishLoadmore();
            }
            ToastUtils.showLong("获取列表数据失败");
        }

        @Override
        public void onErrorFromNet(Throwable ex, boolean isOnCallback) {
            if (mSmartRefreshLayout.isLoading()) {
                mSmartRefreshLayout.finishLoadmore();
            }
            ToastUtils.showLong("获取列表数据失败");
            LogUtils.e(ex);
        }
    };
}
